home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tar.gnu / sprite / tar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-29  |  37.1 KB  |  1,629 lines

  1. /* Tar -- a tape archiver.
  2.  
  3.     Copyright (C) 1988 Free Software Foundation
  4.  
  5. GNU tar is distributed in the hope that it will be useful, but WITHOUT ANY
  6. WARRANTY.  No author or distributor accepts responsibility to anyone
  7. for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.
  9. Refer to the GNU tar General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute GNU tar,
  12. but only under the conditions described in the GNU tar General Public
  13. License.  A copy of this license is supposed to have been given to you
  14. along with GNU tar so you can know your rights and responsibilities.  It
  15. should be in a file named COPYING.  Among other things, the copyright
  16. notice and this notice must be preserved on all copies.
  17.  
  18. In other words, go ahead and share GNU tar, but don't try to stop
  19. anyone else from sharing it farther.  Help stamp out software hoarding!
  20. */
  21.  
  22. /*
  23.  * A tar (tape archiver) program.
  24.  *
  25.  * Written by John Gilmore, ihnp4!hoptoad!gnu, starting 25 Aug 85.
  26.  *
  27.  * @(#)tar.c 1.34 11/6/87 - gnu
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <assert.h>
  32. #include <sys/types.h>        /* Needed for typedefs in tar.h */
  33. #include <sys/stat.h>        /* JF */
  34. #include <sys/param.h>
  35. #include "getopt.h"
  36.  
  37. #ifdef USG
  38. #define rindex strrchr
  39. #endif
  40.  
  41. #ifdef BSD42
  42. #include <sys/dir.h>
  43. #else
  44. #ifdef MSDOS
  45. #include <sys/dir.h>
  46. #else
  47. #ifdef USG
  48. #ifdef NDIR
  49. #include <ndir.h>
  50. #else
  51. #include <dirent.h>
  52. #endif
  53. #ifndef DIRECT
  54. #define direct dirent
  55. #endif
  56. #define DP_NAMELEN(x) strlen((x)->d_name)
  57. #else
  58. /*
  59.  * FIXME: On other systems there is no standard place for the header file
  60.  * for the portable directory access routines.  Change the #include line
  61.  * below to bring it in from wherever it is.
  62.  */
  63. #include "ndir.h"
  64. #endif
  65. #endif
  66. #endif
  67.  
  68. #ifndef DP_NAMELEN
  69. #define DP_NAMELEN(x)    (x)->d_namlen
  70. #endif
  71.  
  72. extern char     *malloc();
  73. extern char     *getenv();
  74. extern char    *strncpy();
  75. extern char    *index();
  76. extern char    *strcpy();    /* JF */
  77. extern char    *strcat();    /* JF */
  78.  
  79. extern char    *optarg;    /* Pointer to argument */
  80. extern int    optind;        /* Global argv index from getopt */
  81.  
  82. /*
  83.  * The following causes "tar.h" to produce definitions of all the
  84.  * global variables, rather than just "extern" declarations of them.
  85.  */
  86. #define TAR_EXTERN /**/
  87. #include "tar.h"
  88.  
  89. /*
  90.  * We should use a conversion routine that does reasonable error
  91.  * checking -- atoi doesn't.  For now, punt.  FIXME.
  92.  */
  93. #define intconv    atoi
  94. extern int    getoldopt();
  95. extern void    read_and();
  96. extern void    list_archive();
  97. extern void    extract_archive();
  98. extern void    diff_archive();
  99. extern void    create_archive();
  100. extern void    update_archive();
  101. extern void    junk_archive();
  102.  
  103. /* JF */
  104. extern time_t    getdate();
  105. #ifdef __GNU__
  106. extern void *init_buffer();
  107. #else
  108. extern char *init_buffer();
  109. #endif
  110. extern char *get_buffer();
  111. extern void add_buffer();
  112. extern void flush_buffer();
  113.  
  114. time_t new_time;
  115.  
  116. static FILE    *namef;        /* File to read names from */
  117. static char    **n_argv;    /* Argv used by name routines */
  118. static int    n_argc;    /* Argc used by name routines */
  119.                 /* They also use "optind" from getopt(). */
  120.  
  121. void    describe();
  122. void    options();
  123.  
  124. #ifndef S_IFLNK
  125. #define lstat stat
  126. #endif
  127.  
  128. #ifndef DEFBLOCKING
  129. #define DEFBLOCKING 20
  130. #endif
  131.  
  132. #ifndef DEF_AR_FILE
  133. #define DEF_AR_FILE "tar.out"
  134. #endif
  135.  
  136. /* For long options that unconditionally set a single flag, we have getopt
  137.    do it.  For the others, we share the code for the equivalent short
  138.    named option, the name of which is stored in the otherwise-unused `val'
  139.    field of the `struct option'; for long options that have no equivalent
  140.    short option, we use nongraphic characters as pseudo short option
  141.    characters, starting (for no particular reason) with character 10. */
  142.  
  143. struct option long_options[] =
  144. {
  145.     {"create",        0,    0,            'c'},
  146.     {"append",        1,    0,            'r'},
  147.     {"extract",        1,    0,            'x'},
  148.     {"get",            1,    0,            'x'},
  149.     {"list",        1,    0,            't'},
  150.     {"update",        1,    0,            'u'},
  151.     {"catenate",        1,    0,            'A'},
  152.     {"concatenate",        1,    0,            'A'},
  153.     {"read-full-blocks",    0,    &f_reblock,        1},
  154. /*     {"directory",        1,    0,            'C'}, */
  155.     {"newer",        0,    0,            13},    /* JF */
  156.     {"newer-mtime",        1,    0,            'N'},    /* JF */
  157.     {"delete",        1,    0,            'D'},
  158.     {"incremental",        0,    0,            'G'},
  159.     {"starting-file",    1,    0,            'K'},
  160.     {"multi-volume",    0,    &f_multivol,        1},
  161.     {"after-date",        1,    0,            'N'},
  162.     {"to-stdout",        0,    &f_exstdout,        1},
  163.     {"record-number",    0,    &f_sayblock,        1},
  164.     {"files-from",        1,    0,            'T'},
  165.     {"volume",        1,    0,            'V'},
  166.     {"verify",        0,    &f_verify,        1},
  167.     {"exclude",        1,    0,            'X'},
  168.     {"block-size",        1,    0,            'b'},
  169.     {"compare",        0,    0,            'd'},
  170.     {"diff",        0,    0,            'd'},
  171.     {"file",        1,    0,            'f'},
  172.     {"dereference",        0,    &f_follow_links,    1},
  173.     {"ignore-zeros",    0,    &f_ignorez,        1},
  174.     {"keep-old-files",    0,    0,            'k'},
  175.     {"one-file-system",    0,    &f_local_filesys,     1},
  176.     {"modification-time",    0,    &f_modified,        1},
  177.     {"old-archive",        0,    0,            'o'},
  178.     {"old",            0,    0,            'o'},
  179.     {"portability",        0,    0,            'o'},
  180.     {"same-permissions",    0,    &f_use_protection,    1},
  181.     {"preserve-permissions",0,    &f_use_protection,    1},
  182.     {"same-order",        0,    &f_sorted_names,    1},
  183.     {"preserve-order",    0,    &f_sorted_names,    1},
  184.     {"preserve",        0,    0,            10},
  185.     {"verbose",         0,    &f_verbose,        1},
  186.     {"interactive",        0,    &f_confirm,        1},
  187.     {"uncompress",        0,    &f_compress,        1},
  188.     {"compress",        0,    &f_compress,        1},
  189.     {"compress-block",    0,    &f_compress,        2},
  190.     {"info-script",        1,    &f_run_script_at_end,    1},
  191.     {"absolute-paths",    0,    &f_absolute_paths,    1},
  192.     {"version",        0,    0,            11},
  193.     {"help",        0,    0,            12},
  194. #ifdef ALLOW_NO_RECURSE    
  195.     {"no_recurse",          0,      &f_no_recurse,          'n'},
  196. #endif
  197.     {"debug",               0,      &f_debug,               'e'},
  198.     {0, 0, 0, 0}
  199. };
  200.  
  201. /*
  202.  * Main routine for tar.
  203.  */
  204. main(argc, argv)
  205.     int    argc;
  206.     char    **argv;
  207. {
  208.  
  209.         assert(sizeof(union record) == RECORDSIZE);
  210.     tar = argv[0];        /* JF: was "tar" Set program name */
  211.  
  212.     options(argc, argv);
  213.  
  214.     name_init(argc, argv);
  215.     switch(cmd_mode) {
  216.     case CMD_CAT:
  217.     case CMD_UPDATE:
  218.     case CMD_APPEND:
  219.         update_archive();
  220.         break;
  221.     case CMD_DELETE:
  222.         junk_archive();
  223.         break;
  224.     case CMD_CREATE:
  225.         create_archive();
  226.         break;
  227.     case CMD_EXTRACT:
  228.         extr_init();
  229.         read_and(extract_archive);
  230.         break;
  231.     case CMD_LIST:
  232.         read_and(list_archive);
  233.         break;
  234.     case CMD_DIFF:
  235.         diff_init();
  236.         read_and(diff_archive);
  237.         break;
  238.     case CMD_NONE:
  239.         fprintf (stderr,"%s: you must specify exactly one of the r, c, t, x, or d options\n", tar);
  240.         fprintf (stderr,"For more information, type ``%s +help''.\n",tar);
  241.         exit(EX_ARGSBAD);
  242.     }
  243.     if (f_debug) {
  244.         fprintf(stderr, "Tar.gnu: Exiting normally\n");
  245.     }
  246.     exit(0);
  247.     /* NOTREACHED */
  248. }
  249.  
  250.  
  251. /*
  252.  * Parse the options for tar.
  253.  */
  254. void
  255. options(argc, argv)
  256.     int    argc;
  257.     char    **argv;
  258. {
  259.     register int    c;        /* Option letter */
  260.     int        ind = -1;
  261.     extern char version_string[];
  262.  
  263.     /* Set default option values */
  264.     blocking = DEFBLOCKING;        /* From Makefile */
  265.     ar_file = getenv("TAPE");    /* From environment, or */
  266.     if (ar_file == 0)
  267.         ar_file = DEF_AR_FILE;    /* From Makefile */
  268.  
  269.     /* Parse options */
  270.     while ((c = getoldopt(argc, argv,
  271.                   "01234567Ab:BcC:dDef:F:GhikK:lLmMnN:oOpPrRsStT:uvV:wWxX:zZ",
  272.                   long_options, &ind)) != EOF) {
  273.         if (c == 0 && long_options[ind].flag == 0)
  274.             c = long_options[ind].val;
  275.         switch (c) {
  276.         case 0:        /* long options that set a single flag */
  277.             break;
  278.         case 10:    /* preserve */
  279.             f_use_protection = f_sorted_names = 1;
  280.             break;
  281.         case 11:    /* version */
  282.             fprintf(stderr,"%s\n",version_string);
  283.             break;
  284.         case 12:    /* help */
  285.             fprintf(stderr,"This is GNU tar, the tape archiving program.\n");
  286.             describe();
  287.             exit(1);
  288.         case 13:
  289.             f_new_files++;
  290.             goto get_newer;
  291.  
  292.         case '0':
  293.         case '1':
  294.         case '2':
  295.         case '3':
  296.         case '4':
  297.         case '5':
  298.         case '6':
  299.         case '7':
  300.             {
  301.                 /* JF this'll have to be modified for other
  302.                    systems, of course! */
  303. #ifndef LOW_NUM
  304. #define LOW_NUM 0
  305. #define MID_NUM 8
  306. #define HGH_NUM 16
  307. #endif
  308.                 int d,add;
  309.                 static char buf[50];
  310.  
  311.                 d=getoldopt(argc,argv,"lmh", 0, 0);
  312.                 if(d=='l') add=LOW_NUM;
  313.                 else if(d=='m') add=MID_NUM;
  314.                 else if(d=='h') add=HGH_NUM;
  315.                 else goto badopt;
  316.                 sprintf(buf,"/dev/rmt%d",add+c-'0');
  317.                 ar_file=buf;
  318.             }
  319.             break;
  320.  
  321.         case 'A':            /* Arguments are tar files,
  322.                            just cat them onto the end
  323.                            of the archive.  */
  324.             if(cmd_mode!=CMD_NONE)
  325.                 goto badopt;
  326.             cmd_mode=CMD_CAT;
  327.             break;
  328.  
  329.         case 'b':            /* Set blocking factor */
  330.             blocking = intconv(optarg);
  331.             break;
  332.  
  333.         case 'B':            /* Try to reblock input */
  334.             f_reblock++;        /* For reading 4.2BSD pipes */
  335.             break;
  336.  
  337.         case 'c':            /* Create an archive */
  338.             if(cmd_mode!=CMD_NONE)
  339.                 goto badopt;
  340.             cmd_mode=CMD_CREATE;
  341.             break;
  342.  
  343.         case 'C':
  344.             if(chdir(optarg)<0)
  345.                 msg_perror("Can't change directory to %d",optarg);
  346.             break;
  347.  
  348.         case 'd':            /* Find difference tape/disk */
  349.             if(cmd_mode!=CMD_NONE)
  350.                 goto badopt;
  351.             cmd_mode=CMD_DIFF;
  352.             break;
  353.  
  354.         case 'D':            /* Delete in the archive */
  355.             if(cmd_mode!=CMD_NONE)
  356.                 goto badopt;
  357.             cmd_mode=CMD_DELETE;
  358.             /* 
  359.              * XXX Need to fix junk_archive to deal with long 
  360.              * names.  
  361.              */
  362.             fprintf(stderr, "Warning: -D is probably flaky.\n");
  363.             break;
  364.  
  365.         case 'e':
  366.             f_debug = 1;
  367.             break;
  368.  
  369.         case 'f':        /* Use ar_file for the archive */
  370.             ar_file = optarg;
  371.             break;
  372.  
  373.         case 'F':
  374.             /* Since -F is only useful with -M , make it implied */
  375.             f_run_script_at_end++;  /* run this script at the end */
  376.             info_script = optarg;    /* of each tape */
  377.             f_multivol++;
  378.             break;
  379.  
  380.         case 'G':            /* We are making a GNU dump; save
  381.                            directories at the beginning of
  382.                            the archive, and include in each
  383.                            directory its contents */
  384.             if(f_oldarch)
  385.                 goto badopt;
  386.             f_gnudump++;
  387.             fprintf(stderr,
  388.                 "Warning: -G may be flaky.\n");    /* XXX */
  389.             break;
  390.  
  391.         case 'h':
  392.             f_follow_links++;    /* follow symbolic links */
  393.             break;
  394.  
  395.         case 'i':
  396.             f_ignorez++;        /* Ignore zero records (eofs) */
  397.             /*
  398.              * This can't be the default, because Unix tar
  399.              * writes two records of zeros, then pads out the
  400.              * block with garbage.
  401.              */
  402.             break;
  403.  
  404.         case 'k':            /* Don't overwrite files */
  405. #ifdef NO_OPEN3
  406.             fprintf(stderr,
  407.                 "%s: can't do -k option on this system\n",tar);
  408.             exit(EX_ARGSBAD);
  409. #else
  410.             f_keep++;
  411. #endif
  412.             break;
  413.  
  414.         case 'K':
  415.             f_startfile++;
  416.             addname(optarg);
  417.             break;
  418.  
  419.         case 'l':            /* When dumping directories, don't
  420.                            dump files/subdirectories that are
  421.                            on other filesystems. */
  422.             f_local_filesys++;
  423.             break;
  424.  
  425.         case 'L':
  426.             if (f_oldarch) {
  427.                 fprintf(stderr,
  428.                     "Can't use long names with V7 tar.\n");
  429.                 goto badopt;
  430.             }
  431. #ifdef ALLOW_LONG_NAMES
  432.             f_long_names++;
  433. #else
  434.             fprintf(stderr,
  435.                 "Warning: tar wasn't configured for long names.\n");
  436. #endif
  437.             break;
  438.  
  439.         case 'm':
  440.             f_modified++;
  441.             break;
  442.  
  443.         case 'M':            /* Make Multivolume archive:
  444.                            When we can't write any more
  445.                            into the archive, re-open it,
  446.                            and continue writing */
  447.             f_multivol++;
  448.             break;
  449.  
  450.         case 'n':            /* Don't recurse into
  451.                            sub directories */
  452.             f_no_recurse++;
  453.             break;
  454.  
  455.         case 'N':            /* Only write files newer than X */
  456.         get_newer:
  457.             f_new_files++;
  458.             new_time=getdate(optarg,(struct timeb *)0);
  459.             break;
  460.  
  461.         case 'o':            /* Generate old archive */
  462.             if(f_gnudump /* || f_dironly */)
  463.                 goto badopt;
  464.             if (f_long_names) {
  465.                 fprintf(stderr,
  466.                     "Can't use long names with V7 tar.\n");
  467.                 goto badopt;
  468.             }
  469.             f_oldarch++;
  470.             break;
  471.  
  472.         case 'O':
  473.             f_exstdout++;
  474.             break;
  475.  
  476.         case 'p':
  477.             f_use_protection++;
  478.             break;
  479.  
  480.         case 'P':
  481.             f_absolute_paths++;
  482.             break;
  483.  
  484.         case 'r':            /* Append files to the archive */
  485.             if(cmd_mode!=CMD_NONE)
  486.                 goto badopt;
  487.             cmd_mode=CMD_APPEND;
  488.             break;
  489.  
  490.         case 'R':
  491.             f_sayblock++;        /* Print block #s for debug */
  492.             break;            /* of bad tar archives */
  493.  
  494.         case 's':
  495.             f_sorted_names++;    /* Names to extr are sorted */
  496.             break;
  497.  
  498.         case 'S':            /* deal with sparse files */
  499.             f_sparse_files++;
  500.             fprintf(stderr,
  501.                 "Warning: support for sparse files is probably broken.");
  502.             fprintf(stderr, "Use at your own risk.");
  503.             break;
  504.         case 't':
  505.             if(cmd_mode!=CMD_NONE)
  506.                 goto badopt;
  507.             cmd_mode=CMD_LIST;
  508.             f_verbose++;        /* "t" output == "cv" or "xv" */
  509.             break;
  510.  
  511.         case 'T':
  512.             name_file = optarg;
  513.             f_namefile++;
  514.             break;
  515.  
  516.         case 'u':            /* Append files to the archive that
  517.                            aren't there, or are newer than the
  518.                            copy in the archive */
  519.             if(cmd_mode!=CMD_NONE)
  520.                 goto badopt;
  521.             cmd_mode=CMD_UPDATE;
  522.             break;
  523.  
  524.         case 'v':
  525.             f_verbose++;
  526.             break;
  527.  
  528.         case 'V':
  529.             f_volhdr=optarg;
  530.             break;
  531.  
  532.         case 'w':
  533.             f_confirm++;
  534.             break;
  535.  
  536.         case 'W':
  537.             f_verify++;
  538.             break;
  539.  
  540.         case 'x':            /* Extract files from the archive */
  541.             if(cmd_mode!=CMD_NONE)
  542.                 goto badopt;
  543.             cmd_mode=CMD_EXTRACT;
  544.             break;
  545.  
  546.         case 'X':
  547.             f_exclude++;
  548.             add_exclude(optarg);
  549.             break;
  550.  
  551.         case 'z':        /* Easy to type */
  552.         case 'Z':        /* Like the filename extension .Z */
  553.             f_compress++;
  554.             break;
  555.  
  556.         case '?':
  557.         default:
  558.         badopt:
  559.             fprintf(stderr, "%s: bad option.  Use '%s +help' for a complete list of options.\n", tar, tar);
  560.             exit(EX_ARGSBAD);
  561.         }
  562.     }
  563.  
  564.     blocksize = blocking * RECORDSIZE;
  565. }
  566.  
  567.  
  568. /*
  569.  * Print as much help as the user's gonna get.
  570.  *
  571.  * We have to sprinkle in the KLUDGE lines because too many compilers
  572.  * cannot handle character strings longer than about 512 bytes.  Yuk!
  573.  * In particular, MSDOS and Xenix MSC and PDP-11 V7 Unix have this
  574.  * problem.
  575.  */
  576. void
  577. describe()
  578. {
  579.     fprintf(stderr,"%s: choose one of the following:\n",tar);
  580.     fputs("\
  581. -A, +catenate        append tar files to an archive\n\
  582. -c, +create        create a new archive\n\
  583. -d, +compare        find differences between archive and file system\n\
  584. -D, +delete        delete from the archive (not for use on mag tapes!)\n\
  585. -r, +append        append files to the end of an archive\n\
  586. -t, +list        list the contents of an archive\n\
  587. -u, +update        only append files that are newer than copy in archive\n\
  588. -x, +extract        extract files from an archive\n",stderr);
  589.  
  590.     fputs("\
  591. Other options:\n\
  592. -b, +block-size N    block size of Nx512 bytes\n\
  593. -B, +read-full-blocks    reblock as we read (for reading 4.2BSD pipes)\n\
  594. -C, +directory dir    change to directory DIR\n\
  595. ", stderr); /* KLUDGE */ fputs("\
  596. -e, +debug              print extra debugging information\n\
  597. -f, +file F        use archive file or device F (or hostname:/dev/file)\n\
  598. -G, +incremental    create/list/extract GNU-format incremental backup\n\
  599. -h, +dereference    don't dump symlinks; dump the files they point to\n\
  600. -i, +ignore-zeros    ignore blocks of zeros in archive (normally mean EOF)\n\
  601. -k, +keep-old-files    keep existing files; don't overwrite them from archive\n\
  602. -K, +starting-file file    begin at FILE in the archive\n\
  603. -l, +one-file-system    stay in local file system when creating an archive\n\
  604. ", stderr); /* KLUDGE */ fputs("\
  605. -L            allow long file names when creating an archive\n\
  606. -m, +modification-time    don't extract file modified time\n\
  607. -M, +multi-volume    create/list/extract multi-volume archive\n\
  608. -n, +no_recurse         do not recurse into directories\n\
  609. -N, +after-date date    only store files newer than DATE\n\
  610. -o, +old-archive    write a  V7 format archive, rather than ANSI format\n\
  611. -O, +to-stdout        extract files to standard output\n\
  612. -p, +same-permissions    extract all protection information\n\
  613. -P, +absolute-paths    don't strip leading \"/\"es from file names\n\
  614. +preserve        like -p -s\n\
  615. -R, +record-number    show record number within archive with each message\n\
  616. -s, +same-order        list of names to extract is sorted to match archive\n\
  617. -S, +sparse-file    handle sparse files specially\n\
  618. ", stderr); /* KLUDGE */ fputs("\
  619. -T, +files-from F    get names to extract or create from file F\n\
  620. -v, +verbose        verbosely list files processed\n\
  621. -V, +volume vnam    create archive with volume name VNAM\n\
  622. +version        print tar program version number\n\
  623. -w, +interactive    ask for confirmation for every action\n\
  624. -W, +verify        attempt to verify the archive after writing it\n\
  625. -X, +exclude file    exclude files listed in FILE\n\
  626. -z,-Z,+compress        filter the archive through compress\n\
  627. -[0-7][lmh]        specify drive and density\n\
  628. ", stderr);
  629. }
  630.  
  631.  
  632. /*
  633.  * Set up to gather file names for tar.
  634.  *
  635.  * They can either come from stdin or from argv.
  636.  */
  637. name_init(argc, argv)
  638.     int    argc;
  639.     char    **argv;
  640. {
  641.  
  642.     if (f_namefile) {
  643.         if (optind < argc) {
  644.             fprintf(stderr, "tar: too many args with -T option\n");
  645.             exit(EX_ARGSBAD);
  646.         }
  647.         if (!strcmp(name_file, "-")) {
  648.             namef = stdin;
  649.         } else {
  650.             namef = fopen(name_file, "r");
  651.             if (namef == NULL) {
  652.                 msg_perror("can't open file %s",name_file);
  653.                 exit(EX_BADFILE);
  654.             }
  655.         }
  656.     } else {
  657.         /* Get file names from argv, after options. */
  658.         n_argc = argc;
  659.         n_argv = argv;
  660.     }
  661. }
  662.  
  663. /*
  664.  * Get the next name from argv or the name file.
  665.  *
  666.  * Result is in static storage and can't be relied upon across two calls.
  667.  */
  668.  
  669. /* C is non-zero if we should deal with -C */
  670. char *
  671. name_next(c)
  672. {
  673.     static char    buffer[MAXPATHLEN+2];    /* Holding pattern */
  674.     register char    *p;
  675.     register char    *q;
  676.     extern char *un_quote_string();
  677.  
  678.  tryagain:
  679.     if (namef == NULL) {
  680.         /* Names come from argv, after options */
  681.         if (optind < n_argc) {
  682.             /* JF trivial support for -C option.  I don't know if
  683.                chdir'ing at this point is dangerous or not.
  684.                It seems to work, which is all I ask. */
  685.             if(c && n_argv[optind][0]=='-' && n_argv[optind][1]=='C' && n_argv[optind][2]=='\0' && n_argv[optind+1]!=0) {
  686.                 optind++;
  687.                 if(chdir(n_argv[optind])<0) {
  688.                     msg_perror("Can't chdir to %s",n_argv[optind]);
  689.                 }
  690.                 optind++;
  691.                 if(optind>=n_argc)
  692.                     return (char *)NULL;
  693.             }
  694.             /* End of JF quick -C hack */
  695.  
  696.             if(f_exclude && check_exclude(n_argv[optind])) {
  697.                 optind++;
  698.                 goto tryagain;
  699.             }
  700.             return un_quote_string(n_argv[optind++]);
  701.         }
  702.         return (char *)NULL;
  703.     }
  704.     while(p = fgets(buffer, MAXPATHLEN+1 /*nl*/, namef)) {
  705.         q = p+strlen(p)-1;        /* Find the newline */
  706.         if (q <= p)            /* Ignore empty lines */
  707.             continue;
  708.         *q-- = '\0';            /* Zap the newline */
  709.         while (q > p && *q == '/')    /* Zap trailing /s */
  710.             *q-- = '\0';
  711.         if(f_exclude && check_exclude(p))
  712.             goto tryagain;
  713.         if (f_debug) {
  714.             fprintf(stderr, "name_next is %s\n", p);
  715.         }
  716.         return un_quote_string(p);
  717.     }
  718.     if (f_debug) {
  719.         fprintf(stderr, "name_next is NULL\n");
  720.     }
  721.     return NULL;
  722. }
  723.  
  724.  
  725. /*
  726.  * Close the name file, if any.
  727.  */
  728. name_close()
  729. {
  730.  
  731.     if (namef != NULL && namef != stdin) fclose(namef);
  732. }
  733.  
  734.  
  735. /*
  736.  * Gather names in a list for scanning.
  737.  * Could hash them later if we really care.
  738.  *
  739.  * If the names are already sorted to match the archive, we just
  740.  * read them one by one.  name_gather reads the first one, and it
  741.  * is called by name_match as appropriate to read the next ones.
  742.  * At EOF, the last name read is just left in the buffer.
  743.  * This option lets users of small machines extract an arbitrary
  744.  * number of files by doing "tar t" and editing down the list of files.
  745.  */
  746. name_gather()
  747. {
  748.     register char *p;
  749.     static struct name namebuf[1];    /* One-name buffer */
  750.     static char *chdir_name;
  751.  
  752.     if (f_sorted_names) {
  753.         p = name_next(0);
  754.         if (p) {
  755.             if(*p=='-' && p[1]=='C' && p[2]=='\0') {
  756.                 chdir_name=name_next(0);
  757.                 p=name_next(0);
  758.                 if(!p) {
  759.                     fprintf(stderr,"Missing file name after -C\n");
  760.                     exit(EX_ARGSBAD);
  761.                 }
  762.                 namebuf[0].change_dir=chdir_name;
  763.             }
  764.             namebuf[0].length = strlen(p);
  765.             if (namebuf[0].length >= sizeof namebuf[0].name) {
  766.                 fprintf(stderr, "Argument name too long: %s\n",
  767.                     p);
  768.                 namebuf[0].length = (sizeof namebuf[0].name) - 1;
  769.             }
  770.             strncpy(namebuf[0].name, p, namebuf[0].length);
  771.             namebuf[0].name[ namebuf[0].length ] = 0;
  772.             namebuf[0].next = (struct name *)NULL;
  773.             namebuf[0].found = 0;
  774.             namelist = namebuf;
  775.             namelast = namelist;
  776.         }
  777.         return;
  778.     }
  779.  
  780.     /* Non sorted names -- read them all in */
  781.     while (p = name_next(0))
  782.         addname(p);
  783. }
  784.  
  785. /*
  786.  * Add a name to the namelist.
  787.  */
  788. addname(name)
  789.     char    *name;            /* pointer to name */
  790. {
  791.     register int    i;        /* Length of string */
  792.     register struct name    *p;    /* Current struct pointer */
  793.     static char *chdir_name;
  794.     char *new_name();
  795. #define MAXPATHLEN 1024
  796.  
  797.     if(name[0]=='-' && name[1]=='C' && name[2]=='\0') {
  798.         chdir_name=name_next(0);
  799.         name=name_next(0);
  800.         if(!name) {
  801.             fprintf(stderr,"Missing file name after -C\n");
  802.             exit(EX_ARGSBAD);
  803.         }
  804.         if(chdir_name[0]!='/') {
  805.             char path[MAXPATHLEN];
  806. #if defined(MSDOS) || defined(USG)
  807.             int getcwd();
  808.  
  809.             if(!getcwd(path,MAXPATHLEN))
  810.                 fprintf(stderr,"Couldn't get current dir\n");
  811.                 exit(EX_SYSTEM);
  812. #else
  813.             char *getwd();
  814.  
  815.             if(!getwd(path)) {
  816.                 fprintf(stderr,"Couldn't get current dir: %s\n",path);
  817.                 exit(EX_SYSTEM);
  818.             }
  819. #endif
  820.             chdir_name=new_name(path,chdir_name);
  821.         }
  822.     }
  823.  
  824.     i = strlen(name);
  825.     /*NOSTRICT*/
  826.     p = (struct name *)
  827.         malloc((unsigned)(i + sizeof(struct name) - NAMSIZ));
  828.     if (!p) {
  829.         fprintf(stderr,"tar: cannot allocate mem for name %s\n",name);
  830.         exit(EX_SYSTEM);
  831.     }
  832.     p->next = (struct name *)NULL;
  833.     p->length = i;
  834.     strncpy(p->name, name, i);
  835.     p->name[i] = '\0';    /* Null term */
  836.     p->found = 0;
  837.     p->regexp = 0;        /* Assume not a regular expression */
  838.     p->firstch = 1;        /* Assume first char is literal */
  839.     p->change_dir=chdir_name;
  840.     p->dir_contents = 0;    /* JF */
  841.     if (index(name, '*') || index(name, '[') || index(name, '?')) {
  842.         p->regexp = 1;    /* No, it's a regexp */
  843.         if (name[0] == '*' || name[0] == '[' || name[0] == '?')
  844.             p->firstch = 0;        /* Not even 1st char literal */
  845.     }
  846.  
  847.     if (namelast) namelast->next = p;
  848.     namelast = p;
  849.     if (!namelist) namelist = p;
  850. }
  851.  
  852. add_dir_to_name(name,dirc)
  853. char *name;
  854. char *dirc;
  855. {
  856.     struct name *n;
  857.  
  858.     for(n=namelist;n;n=n->next) {
  859.         if(!strcmp(n->name,name)) {
  860.             n->dir_contents = dirc;
  861.             return;
  862.         }
  863.     }
  864. }
  865.  
  866. /*
  867.  * Match a name from an archive, p, with a name from the namelist.
  868.  */
  869. name_match(p)
  870.     register char *p;
  871. {
  872.     register struct name    *nlp;
  873.     register int        len;
  874.  
  875. again:
  876.     if (0 == (nlp = namelist))    /* Empty namelist is easy */
  877.         return 1;
  878.     len = strlen(p);
  879.     for (; nlp != 0; nlp = nlp->next) {
  880.         /* If first chars don't match, quick skip */
  881.         if (nlp->firstch && nlp->name[0] != p[0])
  882.             continue;
  883.  
  884.         /* Regular expressions */
  885.         if (nlp->regexp) {
  886.             if (wildmat(p, nlp->name)) {
  887.                 nlp->found = 1;    /* Remember it matched */
  888.                 if(f_startfile) {
  889.                     free((void *)namelist);
  890.                     namelist=0;
  891.                 }
  892.                 if(nlp->change_dir && chdir(nlp->change_dir))
  893.                     msg_perror("Can't change to directory %s\n",nlp->change_dir);
  894.                 return 1;    /* We got a match */
  895.             }
  896.             continue;
  897.         }
  898.  
  899.         /* Plain Old Strings */
  900.         if (nlp->length <= len        /* Archive len >= specified */
  901.          && (p[nlp->length] == '\0' || p[nlp->length] == '/')
  902.                         /* Full match on file/dirname */
  903.          && strncmp(p, nlp->name, nlp->length) == 0) /* Name compare */
  904.         {
  905.             nlp->found = 1;        /* Remember it matched */
  906.             if(f_startfile) {
  907.                 free((void *)namelist);
  908.                 namelist = 0;
  909.             }
  910.             if(nlp->change_dir && chdir(nlp->change_dir))
  911.                 msg_perror("Can't change to directory %s\n",nlp->change_dir);
  912.             return 1;        /* We got a match */
  913.         }
  914.     }
  915.  
  916.     /*
  917.      * Filename from archive not found in namelist.
  918.      * If we have the whole namelist here, just return 0.
  919.      * Otherwise, read the next name in and compare it.
  920.      * If this was the last name, namelist->found will remain on.
  921.      * If not, we loop to compare the newly read name.
  922.      */
  923.     if (f_sorted_names && namelist->found) {
  924.         name_gather();        /* Read one more */
  925.         if (!namelist->found) goto again;
  926.     }
  927.     return 0;
  928. }
  929.  
  930.  
  931. /*
  932.  * Print the names of things in the namelist that were not matched.
  933.  */
  934. names_notfound()
  935. {
  936.     register struct name    *nlp;
  937.     register char        *p;
  938.  
  939.     for (nlp = namelist; nlp != 0; nlp = nlp->next) {
  940.         if (!nlp->found) {
  941.             fprintf(stderr, "tar: %s not found in archive\n",
  942.                 nlp->name);
  943.         }
  944.         /*
  945.          * We could free() the list, but the process is about
  946.          * to die anyway, so save some CPU time.  Amigas and
  947.          * other similarly broken software will need to waste
  948.          * the time, though.
  949.          */
  950. #ifndef unix
  951.         if (!f_sorted_names)
  952.             free(nlp);
  953. #endif
  954.     }
  955.     namelist = (struct name *)NULL;
  956.     namelast = (struct name *)NULL;
  957.  
  958.     if (f_sorted_names) {
  959.         while (0 != (p = name_next(1)))
  960.             fprintf(stderr, "tar: %s not found in archive\n", p);
  961.     }
  962. }
  963.  
  964. /* These next routines were created by JF */
  965.  
  966. name_expand()
  967. {
  968. ;
  969. }
  970. /* p is a directory.  Add all the files in P to the namelist.  If any of the
  971.    files is a directory, recurse on the subdirectory. . . */
  972. static
  973. add_dir_name(p,device)
  974. char *p;
  975. int device;
  976. {
  977.     char *new_buf;
  978.     char *p_buf;
  979.     char *get_dir_contents();
  980.     new_buf=get_dir_contents(p,device);
  981.  
  982.     add_dir_to_name(p,new_buf);
  983.     {
  984.         char namebuf[NAMSIZ+2];
  985.         register int len;
  986.  
  987.         (void)strcpy(namebuf,p);
  988.         len=strlen(namebuf);
  989.         if(namebuf[len-1]!='/') {
  990.             namebuf[len++]='/';
  991.             namebuf[len]='\0';
  992.         }
  993.         for(p_buf=new_buf;*p_buf;p_buf+=strlen(p_buf)+1) {
  994.             if(*p_buf=='D') {
  995.                 (void)strcpy(namebuf+len,p_buf+1);
  996.                 addname(namebuf);
  997.                 add_dir_name(namebuf,device);
  998.             }
  999.         }
  1000.     }
  1001. }
  1002.  
  1003. char *
  1004. get_dir_contents(p,device)
  1005. char *p;
  1006. int device;
  1007. {
  1008.     register DIR *dirp;
  1009.     register struct direct *d;
  1010.     char namebuf[NAMSIZ+2];
  1011.     register int len;
  1012.     extern int errno;
  1013.  
  1014. #ifdef    __GNU__
  1015.     void *the_buffer;
  1016. #else
  1017.     char *the_buffer;
  1018. #endif
  1019.         char *buf,*p_buf;
  1020.         char **vec,**p_vec;
  1021.         int n_strs,n_size;
  1022.         char *new_buf;
  1023.         int dirent_cmp();
  1024.  
  1025.     if(f_local_filesys && device<0) {
  1026.         struct stat hs;
  1027.  
  1028.         if (0 != f_follow_links ? stat(p, &hs) : lstat(p, &hs))
  1029.             msg_perror("can't stat %s",namebuf);
  1030.         else
  1031.             device=hs.st_dev;
  1032.     }
  1033.     errno=0;
  1034.     dirp=opendir(p);
  1035.     if(!dirp) {
  1036.         if(errno)
  1037.             msg_perror("can't open directory %s",p);
  1038.         else
  1039.             msg("error opening directory %s",p);
  1040.         return "\0\0\0\0";
  1041.     }
  1042.     (void) strcpy(namebuf,p);
  1043.     if(p[strlen(p)-1]!='/')
  1044.         (void) strcat(namebuf,"/");
  1045.     len=strlen(namebuf);
  1046.  
  1047.     the_buffer=init_buffer();
  1048.     while(d=readdir(dirp)) {
  1049.         struct stat hs;
  1050.  
  1051.         /* Skip . and .. */
  1052.         if(is_dot_or_dotdot(d->d_name))
  1053.             continue;
  1054.         if(DP_NAMELEN(d) + len >=NAMSIZ) {
  1055.             msg("%s%s name too long: skipped",namebuf,d->d_name);
  1056.             continue;
  1057.         }
  1058.         (void) strcpy(namebuf+len,d->d_name);
  1059.         if (0 != f_follow_links? stat(namebuf, &hs): lstat(namebuf, &hs)) {
  1060.             msg_perror("can't stat %s",namebuf);
  1061.             continue;
  1062.         }
  1063.         if((f_new_files && new_time>hs.st_mtime && (hs.st_mode&S_IFMT)!=S_IFDIR
  1064.             && (f_new_files>1 || new_time>hs.st_ctime) )
  1065.            || (f_local_filesys && device>=0 && device!=hs.st_dev)
  1066.            || (f_exclude && check_exclude(namebuf)))
  1067.             add_buffer(the_buffer,"N",1);
  1068.         else if((hs.st_mode&S_IFMT)==S_IFDIR)
  1069.             add_buffer(the_buffer,"D",1);
  1070.         else
  1071.             add_buffer(the_buffer,"Y",1);
  1072.         add_buffer(the_buffer,d->d_name,(int)(DP_NAMELEN(d)+1));
  1073.     }
  1074.     add_buffer(the_buffer,"\000\000",2);
  1075.     closedir(dirp);
  1076.  
  1077.     /* Well, we've read in the contents of the dir, now sort them */
  1078.     buf=get_buffer(the_buffer);
  1079.     n_strs=0;
  1080.     n_size=0;
  1081.     for(p_buf=buf;*p_buf;) {
  1082.         int tmp;
  1083.  
  1084.         tmp=strlen(p_buf)+1;
  1085.         n_strs++;
  1086.         n_size+=tmp;
  1087.         p_buf+=tmp;
  1088.     }
  1089.     if(n_strs==0) {
  1090.         flush_buffer(the_buffer);
  1091.         return "\0\0\0\0";
  1092.     }
  1093.     vec=(char **)malloc(sizeof(char *)*(n_strs+1));
  1094.     for(p_vec=vec,p_buf=buf;*p_buf;p_buf+=strlen(p_buf)+1)
  1095.         *p_vec++= p_buf;
  1096.     *p_vec= 0;
  1097.     qsort((void *)vec,n_strs,sizeof(char *),dirent_cmp);
  1098.     new_buf=(char *)malloc(n_size+2);
  1099.     for(p_vec=vec,p_buf=new_buf;*p_vec;p_vec++) {
  1100.         char *p_tmp;
  1101.  
  1102.         for(p_tmp= *p_vec;*p_buf++= *p_tmp++;)
  1103.             ;
  1104.     }
  1105.     *p_buf++='\0';
  1106.     free(vec);
  1107.     flush_buffer(the_buffer);
  1108.     return new_buf;
  1109. }
  1110.  
  1111. int dirent_cmp(p1,p2)
  1112. char **p1,**p2;
  1113. {
  1114.     char *frst,*scnd;
  1115.  
  1116.     frst= (*p1)+1;
  1117.     scnd= (*p2)+1;
  1118.  
  1119.     return strcmp(frst,scnd);
  1120. }
  1121.  
  1122. /* This is like name_match(), except that it returns a pointer to the name
  1123.    it matched, and doesn't set ->found  The caller will have to do that
  1124.    if it wants to.  Oh, and if the namelist is empty, it returns 0, unlike
  1125.    name_match(), which returns TRUE */
  1126.  
  1127. struct name *
  1128. name_scan(p)
  1129. register char *p;
  1130. {
  1131.     register struct name    *nlp;
  1132.     register int        len;
  1133.  
  1134. again:
  1135.     if (0 == (nlp = namelist))    /* Empty namelist is easy */
  1136.         return 0;
  1137.     len = strlen(p);
  1138.     for (; nlp != 0; nlp = nlp->next) {
  1139.         /* If first chars don't match, quick skip */
  1140.         if (nlp->firstch && nlp->name[0] != p[0])
  1141.             continue;
  1142.  
  1143.         /* Regular expressions */
  1144.         if (nlp->regexp) {
  1145.             if (wildmat(p, nlp->name))
  1146.                 return nlp;    /* We got a match */
  1147.             continue;
  1148.         }
  1149.  
  1150.         /* Plain Old Strings */
  1151.         if (nlp->length <= len        /* Archive len >= specified */
  1152.          && (p[nlp->length] == '\0' || p[nlp->length] == '/')
  1153.                         /* Full match on file/dirname */
  1154.          && strncmp(p, nlp->name, nlp->length) == 0) /* Name compare */
  1155.             return nlp;        /* We got a match */
  1156.     }
  1157.  
  1158.     /*
  1159.      * Filename from archive not found in namelist.
  1160.      * If we have the whole namelist here, just return 0.
  1161.      * Otherwise, read the next name in and compare it.
  1162.      * If this was the last name, namelist->found will remain on.
  1163.      * If not, we loop to compare the newly read name.
  1164.      */
  1165.     if (f_sorted_names && namelist->found) {
  1166.         name_gather();        /* Read one more */
  1167.         if (!namelist->found) goto again;
  1168.     }
  1169.     return (struct name *) 0;
  1170. }
  1171.  
  1172. /* This returns a name from the namelist which doesn't have ->found set.
  1173.    It sets ->found before returning, so successive calls will find and return
  1174.    all the non-found names in the namelist */
  1175.  
  1176. struct name *gnu_list_name;
  1177.  
  1178. char *
  1179. name_from_list()
  1180. {
  1181.     if(!gnu_list_name)
  1182.         gnu_list_name = namelist;
  1183.     while(gnu_list_name && gnu_list_name->found)
  1184.         gnu_list_name=gnu_list_name->next;
  1185.     if(gnu_list_name) {
  1186.         gnu_list_name->found++;
  1187.         if(gnu_list_name->change_dir)
  1188.             if(chdir(gnu_list_name->change_dir)<0)
  1189.                 msg_perror("can't chdir to %s",gnu_list_name->change_dir);
  1190.         return gnu_list_name->name;
  1191.     }
  1192.     return (char *)0;
  1193. }
  1194.  
  1195. blank_name_list()
  1196. {
  1197.     struct name *n;
  1198.  
  1199.     gnu_list_name = 0;
  1200.     for(n=namelist;n;n=n->next)
  1201.         n->found = 0;
  1202. }
  1203.  
  1204. /* Collect all the names from argv[] (or whatever), then expand them into
  1205.    a directory tree, and put all the directories at the beginning. */
  1206. collect_and_sort_names()
  1207. {
  1208.     struct name *n,*n_next;
  1209.     int num_names;
  1210.     int name_cmp();
  1211.     char *merge_sort();
  1212.  
  1213.     name_gather();
  1214.  
  1215.     if(!namelist) addname(".");
  1216.     for(n=namelist;n;n=n_next) {
  1217.         n_next=n->next;
  1218.         if(n->found || n->dir_contents)
  1219.             continue;
  1220.         if(n->regexp)        /* FIXME just skip regexps for now */
  1221.             continue;
  1222.         if(n->change_dir)
  1223.             if(chdir(n->change_dir)<0) {
  1224.                 msg_perror("can't chdir to %s",n->change_dir);
  1225.                 continue;
  1226.             }
  1227.  
  1228.         if(is_a_directory(n->name)) {
  1229.             n->found++;
  1230.             add_dir_name(n->name,-1);
  1231.         }
  1232.     }
  1233.  
  1234.     num_names=0;
  1235.     for(n=namelist;n;n=n->next)
  1236.         num_names++;
  1237.     namelist=(struct name *)merge_sort((void *)namelist,num_names,(char *)(&(namelist->next))-(char *)namelist,name_cmp);
  1238.  
  1239.     for(n=namelist;n;n=n->next) {
  1240.         n->found=0;
  1241.     }
  1242. }
  1243.  
  1244. int name_cmp(n1,n2)
  1245. struct name *n1,*n2;
  1246. {
  1247.     if(n1->found) {
  1248.         if(n2->found)
  1249.             return strcmp(n1->name,n2->name);
  1250.         else
  1251.             return -1;
  1252.     } else if(n2->found)
  1253.         return 1;
  1254.     else
  1255.         return strcmp(n1->name,n2->name);
  1256. }
  1257.  
  1258. char *new_name();
  1259.  
  1260. gnu_restore(name)
  1261. char *name;
  1262. {
  1263.     char *current_dir;
  1264. /*    int current_dir_length; */
  1265.  
  1266.     char *archive_dir;
  1267. /*    int archive_dir_length; */
  1268. #ifdef __GNU__
  1269.     void *the_buffer;
  1270. #else
  1271.     char *the_buffer;
  1272. #endif
  1273.     char    *p;
  1274.     DIR    *dirp;
  1275.     struct direct *d;
  1276.     char *cur,*arc;
  1277.     extern struct stat hstat;        /* Stat struct corresponding */
  1278.     long size,copied;
  1279.     char *from,*to;
  1280.  
  1281.     dirp=opendir(name);
  1282.  
  1283.     if(!dirp) {
  1284.             /* The directory doesn't exist now.  It'll be created.
  1285.                In any case, we don't have to delete any files out
  1286.                of it */
  1287.         return;
  1288.     }
  1289.  
  1290.     the_buffer=init_buffer();
  1291.     while(d=readdir(dirp)) {
  1292.         if(is_dot_or_dotdot(d->d_name))
  1293.             continue;
  1294.  
  1295.         add_buffer(the_buffer,d->d_name,(int)(DP_NAMELEN(d)+1));
  1296.     }
  1297.     closedir(dirp);
  1298.     add_buffer(the_buffer,"",1);
  1299.  
  1300.     current_dir=get_buffer(the_buffer);
  1301.     archive_dir=(char *)malloc(hstat.st_size);
  1302.     if(archive_dir==0) {
  1303.         fprintf(stderr,"Can't allocate %d bytes for restore\n",hstat.st_size);
  1304.         return;
  1305.     }
  1306.     to=archive_dir;
  1307.     for(size=hstat.st_size;size>0;size-=copied) {
  1308.         from=findrec()->charptr;
  1309.         if(!from) {
  1310.             msg("Unexpected EOF in archive\n");
  1311.             break;
  1312.         }
  1313.         copied=endofrecs()->charptr - from;
  1314.         if(copied>size)
  1315.             copied=size;
  1316.         bcopy((void *)from,(void *)to,(int)copied);
  1317.         to+=copied;
  1318.         userec((union record *)(from+copied-1));
  1319.     }
  1320.  
  1321.     for(cur=current_dir;*cur;cur+=strlen(cur)+1) {
  1322.         for(arc=archive_dir;*arc;arc+=strlen(arc)+1) {
  1323.             arc++;
  1324.             if(!strcmp(arc,cur))
  1325.                 break;
  1326.         }
  1327.         if(*arc=='\0') {
  1328.             p=new_name(name,cur);
  1329.             if(f_confirm && !confirm("delete",p)) {
  1330.                 free(p);
  1331.                 continue;
  1332.             }
  1333.             if(f_verbose)
  1334.                 printf("%s: deleting %s\n",p);
  1335.             if(recursively_delete(p)) {
  1336.                 msg("%s: Error while deleting\n",p);
  1337.             }
  1338.             free(p);
  1339.         }
  1340.  
  1341.     }
  1342.     flush_buffer(the_buffer);
  1343.     free(archive_dir);
  1344. }
  1345.  
  1346. recursively_delete(path)
  1347. char *path;
  1348. {
  1349.     struct stat sbuf;
  1350.     DIR *dirp;
  1351.     struct direct *dp;
  1352.     char *path_buf;
  1353.     /* int path_len; */
  1354.  
  1355.  
  1356.     if(lstat(path,&sbuf)<0)
  1357.         return 1;
  1358.     if((sbuf.st_mode &S_IFMT)==S_IFDIR) {
  1359.  
  1360.         /* path_len=strlen(path); */
  1361.         dirp=opendir(path);
  1362.         if(dirp==0)
  1363.             return 1;
  1364.         while(dp=readdir(dirp)) {
  1365.             if(is_dot_or_dotdot(dp->d_name))
  1366.                 continue;
  1367.             path_buf=new_name(path,dp->d_name);
  1368.             if(recursively_delete(path_buf)) {
  1369.                 free(path_buf);
  1370.                 closedir(dirp);
  1371.                 return 1;
  1372.             }
  1373.             free(path_buf);
  1374.         }
  1375.         closedir(dirp);
  1376.  
  1377.         if(rmdir(path)<0)
  1378.             return 1;
  1379.         return 0;
  1380.     }
  1381.     if(unlink(path)<0)
  1382.         return 1;
  1383.     return 0;
  1384. }
  1385.  
  1386. char *
  1387. new_name(path,name)
  1388. char *path,*name;
  1389. {
  1390.     char *path_buf;
  1391.  
  1392.     path_buf=(char *)malloc(strlen(path)+strlen(name)+2);
  1393.     if(path_buf==0) {
  1394.         fprintf(stderr,"Can't allocate memory for name '%s/%s\n",path,name);
  1395.         exit(EX_SYSTEM);
  1396.     }
  1397.     (void) sprintf(path_buf,"%s/%s",path,name);
  1398.     return path_buf;
  1399. }
  1400.  
  1401. /* return non-zero if p is the name of a directory */
  1402. is_a_directory(p)
  1403. char *p;
  1404. {
  1405.     struct stat stbuf;
  1406.  
  1407.     if(lstat(p,&stbuf)<0) {
  1408.         msg_perror("can't stat %s",p);
  1409.         return 0;
  1410.     }
  1411.     if((stbuf.st_mode&S_IFMT)==S_IFDIR)
  1412.         return 1;
  1413.     return 0;
  1414. }
  1415.  
  1416. /* Returns non-zero if p is . or ..   This could be a macro for speed. */
  1417. is_dot_or_dotdot(p)
  1418. char *p;
  1419. {
  1420.     return (p[0]=='.' && (p[1]=='\0' || (p[1]=='.' && p[2]=='\0')));
  1421. }
  1422.  
  1423. /* returns non-zero if the luser typed 'y' or 'Y', zero otherwise. */
  1424.  
  1425. int
  1426. confirm(action,file)
  1427. char *action, *file;
  1428. {
  1429.     int    c,nl;
  1430.     static FILE *confirm_file = 0;
  1431.     extern FILE *msg_file;
  1432.     extern char TTY_NAME[];
  1433.  
  1434.     fprintf(msg_file,"%s %s?", action, file);
  1435.     fflush(msg_file);
  1436.     if(!confirm_file) {
  1437.         confirm_file = (archive == 0) ? fopen(TTY_NAME, "r") : stdin;
  1438.         if(!confirm_file) {
  1439.             fprintf(stderr,"Can't read confirmation from user\n");
  1440.             exit(EX_SYSTEM);
  1441.         }
  1442.     }
  1443.     c=getc(confirm_file);
  1444.     for(nl = c; nl != '\n' && nl != EOF; nl = getc(confirm_file))
  1445.         ;
  1446.     return (c=='y' || c=='Y');
  1447. }
  1448.  
  1449. char *x_buffer = 0;
  1450. int size_x_buffer;
  1451. int free_x_buffer;
  1452.  
  1453. char **exclude = 0;
  1454. int size_exclude = 0;
  1455. int free_exclude = 0;
  1456.  
  1457. char **re_exclude = 0;
  1458. int size_re_exclude = 0;
  1459. int free_re_exclude = 0;
  1460.  
  1461. add_exclude(file)
  1462. char *file;
  1463. {
  1464.     FILE *fp;
  1465.     char buf[1024];
  1466.     extern char *rindex();
  1467.  
  1468.     if(strcmp(file, "-"))
  1469.         fp=fopen(file,"r");
  1470.     else
  1471.         /* Let's hope the person knows what they're doing. */
  1472.         /* Using -X - -T - -f - will get you *REALLY* strange
  1473.            results. . . */
  1474.         fp=stdin;
  1475.  
  1476.     if(!fp) {
  1477.         msg_perror("can't open %s",file);
  1478.         exit(2);
  1479.     }
  1480.     while(fgets(buf,1024,fp)) {
  1481.         int size_buf;
  1482.         char *end_str;
  1483.  
  1484.         end_str=rindex(buf,'\n');
  1485.         *end_str='\0';
  1486.  
  1487.         un_quote_string(buf);
  1488.         size_buf = strlen(buf);
  1489.  
  1490.         if(x_buffer==0) {
  1491.             x_buffer = (char *)ck_malloc(size_buf+1024);
  1492.             free_x_buffer=1024;
  1493.         } else if(free_x_buffer<=size_buf) {
  1494.             char *old_x_buffer;
  1495.             char **tmp_ptr;
  1496.  
  1497.             old_x_buffer = x_buffer;
  1498.             x_buffer = (char *)ck_realloc(x_buffer,size_x_buffer+1024);
  1499.             free_x_buffer = 1024;
  1500.             for(tmp_ptr=exclude;tmp_ptr<exclude+size_exclude;tmp_ptr++)
  1501.                 *tmp_ptr= x_buffer + ((*tmp_ptr) - old_x_buffer);
  1502.             for(tmp_ptr=re_exclude;tmp_ptr<re_exclude+size_re_exclude;tmp_ptr++)
  1503.                 *tmp_ptr= x_buffer + ((*tmp_ptr) - old_x_buffer);
  1504.         }
  1505.  
  1506.         if(is_regex(buf)) {
  1507.             if(free_re_exclude==0) {
  1508.                 re_exclude= (char **)(re_exclude ? ck_realloc(re_exclude,(size_re_exclude+32)*sizeof(char *)) : ck_malloc(sizeof(char *)*32));
  1509.                 free_re_exclude+=32;
  1510.             }
  1511.             re_exclude[size_re_exclude]=x_buffer+size_x_buffer;
  1512.             size_re_exclude++;
  1513.             free_re_exclude--;
  1514.         } else {
  1515.             if(free_exclude==0) {
  1516.                 exclude=(char **)(exclude ? ck_realloc(exclude,(size_exclude+32)*sizeof(char *)) : ck_malloc(sizeof(char *)*32));
  1517.                 free_exclude+=32;
  1518.             }
  1519.             exclude[size_exclude]=x_buffer+size_x_buffer;
  1520.             size_exclude++;
  1521.             free_exclude--;
  1522.         }
  1523.         strcpy(x_buffer+size_x_buffer,buf);
  1524.         size_x_buffer+=size_buf+1;
  1525.         free_x_buffer-=size_buf+1;
  1526.     }
  1527.     fclose(fp);
  1528. }
  1529.  
  1530. int
  1531. is_regex(str)
  1532. char *str;
  1533. {
  1534.     return index(str,'*') || index(str,'[') || index(str,'?');
  1535. }
  1536.  
  1537. /* Returns non-zero if the file 'name' should not be added/extracted */
  1538. int
  1539. check_exclude(name)
  1540. char *name;
  1541. {
  1542.     int n;
  1543.  
  1544.     for(n=0;n<size_re_exclude;n++) {
  1545.         if(wildmat(name,re_exclude[n]))
  1546.             return 1;
  1547.     }
  1548.     for(n=0;n<size_exclude;n++) {
  1549.         if(strstr(name,exclude[n]))
  1550.             return 1;
  1551.     }
  1552.     return 0;
  1553. }
  1554.  
  1555.  
  1556. /*
  1557.  *----------------------------------------------------------------------
  1558.  *
  1559.  * gnu_extended_header --
  1560.  *
  1561.  *    Decide whether the current header is followed by extended header 
  1562.  *    records.
  1563.  *
  1564.  * Results:
  1565.  *    Returns non-zero if the current header is followed by extended
  1566.  *    header records.  Returns zero if not.
  1567.  *
  1568.  * Side effects:
  1569.  *    Exits if the header claims to be extended but lacks the right magic 
  1570.  *    number.  (This is an attempt to detect Posix headers with long 
  1571.  *    names, which we don't know how to handle.)
  1572.  *
  1573.  *----------------------------------------------------------------------
  1574.  */
  1575.  
  1576. int
  1577. gnu_extended_header(myHdr)
  1578.     union record *myHdr;    /* the header to look at */
  1579. {
  1580.     if (!myHdr->header.isextended) {
  1581.         return 0;
  1582.     } else if (myHdr->header.hdr_magic == HDR_MAGIC) {
  1583.         return 1;
  1584.     } else {
  1585.         msg("header has wrong magic number--Posix header, maybe?");
  1586.         exit(1);
  1587.         return 0;    /* lint */
  1588.     }
  1589. }
  1590.  
  1591.  
  1592. /*
  1593.  *----------------------------------------------------------------------
  1594.  *
  1595.  * extended_header_type_name --
  1596.  *
  1597.  *    Get a printable name for the type of an extended header record.
  1598.  *
  1599.  * Results:
  1600.  *    Returns the type, as a printable string.
  1601.  *
  1602.  * Side effects:
  1603.  *    None.
  1604.  *
  1605.  *----------------------------------------------------------------------
  1606.  */
  1607.  
  1608. char *
  1609. extended_header_type_name(type)
  1610.     unsigned char type;
  1611. {
  1612.     switch (type) {
  1613.     case XH_FILENAME:
  1614.         return "file name";
  1615.         break;
  1616.     case XH_LINKNAME:
  1617.         return "link name";
  1618.         break;
  1619.     case XH_SPARSE_FILE:
  1620.         return "sparse file";
  1621.         break;
  1622.     default:
  1623.         panic("bogus extended header type: %d\n", type);
  1624.         break;
  1625.     }
  1626.  
  1627.     /* NOTREACHED */
  1628. }
  1629.